<?php
class ProductController extends Yaf_Controller_Abstract {

   	public function init(){
         if(is_null(Yaf_Session::getInstance()->get(AK47))){
            $this->redirect('/index.php/admin/login/login');
         }
         if ($this->getRequest()->isXmlHttpRequest()) {
            Yaf_Dispatcher::getInstance()->disableView();
         }
   		$this->setViewPath(APP_PATH . '/application/modules/Admin/views');
         $data = array(
               'right_title'  => '事业管理',
               "title"        => '惠信后台管理',
               'content_nav'  => array(
                  array('name'=>'事业列表','href'=>'/index.php/admin/product/index'),
                  array('name'=>'添加事业','href'=>'/index.php/admin/product/add'),
               ),
               'product_li_active' => "class='active'"
            );
         $this->getView()->assign($data);
   	}

      /**
       * 事业列表
       * @return [type] [description]
       */
      public function indexAction(){
         $product_model = new productModel();
         $select_res = $product_model->select(array(),array('order'=>1));
         $data = array(
            'active' => 0,
            'list' => $select_res,
         );
         $this->getView()->assign($data);
      }

      public function addAction(){
         $data = array(
            'active' => 1,
         );
         $this->getView()->assign($data);
      }

      public function ajax_add_checkAction(){
         $post_data = $this->getRequest()->getPost();
         $product_model = new productModel();
         if(empty($post_data['_id'])){
            unset($post_data['_id']);
            //这里还可以查一下重复，先省略
            $res = $product_model->insert($post_data);
            if(isset($res) && ($res['ok'] == 1)){
               exit(json_encode(array('flag'=>true,'msg'=>'添加成功')));
            }
         }
         exit(json_encode(array('flag'=>false,'msg'=>'操作失败')));
      }

      public function ajax_delAction(){
         $_id = $this->getRequest()->getQuery('_id');
         $product_model = new productModel();
         $del_res = $product_model->delete(array('_id'=>$_id));
         if(isset($del_res) && ($del_res['ok'] == 1)){
            exit(
                  json_encode(array('flag'=>true,'msg'=>$_id))
               );
         }
         exit(
                  json_encode(array('flag'=>false,'msg'=>'删除失败'))
               );
      }
}

?>